/*
 * Copyright 2011-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.mgmt.commons.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 其他工具类
 *
 * @author liym
 * @since 2016-07-20 10:43:52
 * @since 2019-03-02 15:48 新建
 */
public final class Utils {

    /**
     * 电子邮箱地址格式正则表达式
     */
    private static final String REGEX_EMAIL = "^([a-zA-Z0-9]*[-_.]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[.][A-Za-z]{2,3}([.][A-Za-z]{2})?$";

    /**
     * 手机格式正则表达式
     */
    private static final String REGEX_MOBILE_PHONE_NO = "^((861)|(1))\\d{10}$";

    /**
     * 身份证格式正则表达式
     */
    private static final String REGEX_IDENT_NUMBER = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";

    /**
     * 十六进制字符
     */
    private final static byte[] HEX = "0123456789abcdef".getBytes();

    private Utils() {}

    /**
     * 将字符串转换成 int 类型，默认为 0。
     *
     * @param str numerical str
     * @return int
     * @author liym
     * @version 2016-04-28 11:21:36
     */
    public static int safeToInt(Object str) {
        String ss = String.valueOf(str);
        int val = 0;
        try {
            val = Integer.parseInt(ss);
        } catch (NumberFormatException ignored) {
        }
        return val;
    }

    /**
     * 将字符串转换成 {@link Integer} 类型，默认为null。
     *
     * @param str numerical str
     * @return int
     * @author liym
     * @version 2016-04-28 11:23:32
     */
    public static Integer safeToInteger(Object str) {
        String ss = String.valueOf(str);
        Integer val;
        try {
            val = Integer.parseInt(ss);
        } catch (NumberFormatException e) {
            val = null;
        }
        return val;
    }

    /**
     * 验证手机号码是否符合基本格式
     *
     * @param mobile 手机号码
     * @return true/false
     */
    public static boolean isMobilePhoneNo(String mobile) {
        Pattern p = Pattern.compile(REGEX_MOBILE_PHONE_NO);
        Matcher m = p.matcher(mobile);
        return m.matches();
    }

    /**
     * 验证邮箱是否符合格式
     *
     * @param email Email
     * @return true/false
     */
    public static boolean isEmail(String email) {
        Pattern p = Pattern.compile(REGEX_EMAIL);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 验证身份证是否符合格式
     *
     * @param idNumber 身份证号码
     * @return true/false
     */
    public static boolean isIdentNumber(String idNumber) {
        Pattern p = Pattern.compile(REGEX_IDENT_NUMBER);
        Matcher m = p.matcher(idNumber);
        return m.matches();
    }

    /**
     * 4位十六进制随机数
     *
     * @return xxxx
     * @author liym
     * @version 2016-07-22 15:00:36
     */
    public static String rnd4() {
        double d1 = 1 + Math.random();
        double d2 = 65536 * d1;
        double d3 = Math.floor(d2);
        String hexString = Integer.toString((int) d3, 16);
        return hexString.substring(1);
    }

    /**
     * 不含横线的UUID
     *
     * @return UUID
     * @author liym
     * @version 2016-07-22 15:00:46
     */
    public static String shortUUID() {
        return rnd4() + rnd4() + rnd4() + rnd4() + rnd4() + rnd4() + rnd4() + rnd4();
    }

    /**
     * 含有横线的UUID
     *
     * @return UUID
     * @author liym
     * @version 2016-07-22 15:00:49
     */
    public static String longUUID() {
        return rnd4() + rnd4() + "-" + rnd4() + "-" + rnd4() + "-" + rnd4() + "-" + rnd4() + rnd4() + rnd4();
    }

    public static void main(String[] args) {
        System.out.println(isEmail("me.cn@gmail.com"));
    }

}
